﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CarbonExchange.Tests
{
    [TestClass]
    public class AmeeProfileItemTest
    {
        [TestMethod]
        public void AmeeProfileItemTest_GetByPathTest()
        {
            string profileId = "1E98F2CC10E2";
            string itemId = "3FCD6BFB7A1B";
            string dataItemId = "D56D9BD67525";
            var factory = new CarbonExchange.Bll.AmeeProfileItemFactory();
            var tmp = factory.GetByPath(profileId, "/home/waste", itemId);
            Assert.IsNotNull(tmp);
            Assert.AreEqual(itemId, tmp.Id);
            Assert.AreEqual(dataItemId, tmp.DataItemId);
            Assert.AreEqual(profileId, tmp.ProfileId);
            Assert.AreEqual(string.Concat("/home/waste/", itemId), tmp.Path);
            Assert.AreEqual(6.04m, tmp.AmountPerMonth);
            Assert.AreEqual("landfill", tmp.GetValue<string>("savingType"));
            Assert.AreEqual(23.5m, tmp.GetValue<decimal>("kgPerMonth"));
        }

        [TestMethod]
        public void AmeeProfileItemTest_UpdateTest()
        {
            string profileId = "1E98F2CC10E2";
            string itemId = "3FCD6BFB7A1B";
            decimal expectedAmountPerMonth1 = 0.258071m;
            decimal expectedKgPerMonth1 = 12.05m;
            decimal expectedAmountPerMonth2 = 0.279488m;
            decimal expectedKgPerMonth2 = 13.05m;

            var factory = new CarbonExchange.Bll.AmeeProfileItemFactory();
            var tmp = factory.GetByPath(profileId, "/home/waste", itemId);

            Assert.IsNotNull(tmp);

            decimal expectedKgPerMonth = expectedKgPerMonth1;
            decimal expectedAmountPerMonth = expectedAmountPerMonth1;
            if (tmp.GetValue<decimal>("kgPerMonth") == expectedKgPerMonth1)
            {
                expectedKgPerMonth = expectedKgPerMonth2;
                expectedAmountPerMonth = expectedAmountPerMonth2;
            }
            tmp.SetValue("kgPerMonth", expectedKgPerMonth);

            factory.Save(tmp);

            Assert.AreEqual(expectedKgPerMonth, tmp.GetValue<decimal>("kgPerMonth"));
            Assert.AreEqual(expectedAmountPerMonth, tmp.AmountPerMonth);

            var tmp2 = factory.GetByPath(profileId, "/home/waste", itemId);

            Assert.AreEqual(tmp2.GetValue<decimal>("kgPerMonth"), tmp.GetValue<decimal>("kgPerMonth"));
            Assert.AreEqual(tmp2.AmountPerMonth, tmp.AmountPerMonth);

        }

        [TestMethod]
        public void AmeeProfileItemTest_InsertTest()
        {
            string profileId = "1E98F2CC10E2";
            var factory = new CarbonExchange.Bll.AmeeProfileItemFactory();
            var dataItemFactory = new CarbonExchange.Bll.AmeeDataCatagoryFactory();
            var dataItem = dataItemFactory.GetByPath("/home/waste", "type=general household");

            Assert.IsNotNull(dataItem);
            Assert.IsTrue(dataItem.DataItemSelected);

            var tmp = factory.CreateItem(dataItem, profileId, DateTime.Now.ToString("f"), DateTime.Parse("24/10/1975"));
            
            Assert.IsNotNull(tmp);
            Assert.IsNull(tmp.Id);
            Assert.AreEqual(dataItem.Id, tmp.DataItemId);
            Assert.AreEqual(profileId, tmp.ProfileId);

            tmp.SetValue("savingType", "landfill");
            tmp.SetValue("kgPerMonth", 23.5m);

            factory.Save(tmp);

            Assert.IsNotNull(tmp.Id);
            Assert.AreEqual(23.5m, tmp.GetValue<decimal>("kgPerMonth"));

            var tmp2 = factory.GetByPath(profileId, "/home/waste", tmp.Id);
            Assert.IsNotNull(tmp2);

            Assert.AreEqual(tmp2.AmountPerMonth, tmp.AmountPerMonth);
            Acms.Bll.Tests.AssertHelper.AreEqual(tmp2.Created, tmp.Created, 0);
            Assert.AreEqual(tmp2.DataItemId, tmp.DataItemId);
            Assert.AreEqual(tmp2.Id, tmp.Id);
            Assert.AreEqual(tmp2.Name, tmp.Name);
            Assert.AreEqual(tmp2.Path, tmp.Path);
            Assert.AreEqual(tmp2.ProfileId, tmp.ProfileId);
            Assert.AreEqual(tmp2.ValidFrom, tmp.ValidFrom);
            Assert.AreEqual(tmp2.GetValue<string>("landfill"), tmp.GetValue<string>("landfill"));
            Assert.AreEqual(tmp2.GetValue<decimal>("kgPerMonth"), tmp.GetValue<decimal>("kgPerMonth"));
        }

        [TestMethod]
        public void AmeeProfileItemTest_Delete()
        {
            string profileId = "1E98F2CC10E2";
            var factory = new CarbonExchange.Bll.AmeeProfileItemFactory();
            var dataItemFactory = new CarbonExchange.Bll.AmeeDataCatagoryFactory();
            var dataItem = dataItemFactory.GetByPath("/home/waste", "type=general household");

            Assert.IsNotNull(dataItem);
            Assert.IsTrue(dataItem.DataItemSelected);

            var tmp = factory.CreateItem(dataItem, profileId, DateTime.Now.ToString("f"), DateTime.Parse("24/10/1975"));

            Assert.IsNotNull(tmp);
            Assert.IsNull(tmp.Id);
            Assert.AreEqual(dataItem.Id, tmp.DataItemId);
            Assert.AreEqual(profileId, tmp.ProfileId);

            tmp.SetValue("savingType", "landfill");
            tmp.SetValue("kgPerMonth", 23.5m);

            factory.Save(tmp);

            Assert.IsNotNull(tmp.Id);

            var tmp2 = factory.GetByPath(profileId, "/home/waste", tmp.Id);
            Assert.IsNotNull(tmp2);

            factory.Delete(tmp);

            var tmp3 = factory.GetByPath(profileId, "/home/waste", tmp.Id);
            Assert.IsNull(tmp3);
        }

        [TestMethod]
        public void AmeeProfileItemTest_DeleteById()
        {
            string profileId = "1E98F2CC10E2";
            var factory = new CarbonExchange.Bll.AmeeProfileItemFactory();
            var dataItemFactory = new CarbonExchange.Bll.AmeeDataCatagoryFactory();
            var dataItem = dataItemFactory.GetByPath("/home/waste", "type=general household");

            Assert.IsNotNull(dataItem);
            Assert.IsTrue(dataItem.DataItemSelected);

            var tmp = factory.CreateItem(dataItem, profileId, DateTime.Now.ToString("f"), DateTime.Parse("24/10/1975"));

            Assert.IsNotNull(tmp);
            Assert.IsNull(tmp.Id);
            Assert.AreEqual(dataItem.Id, tmp.DataItemId);
            Assert.AreEqual(profileId, tmp.ProfileId);

            tmp.SetValue("savingType", "landfill");
            tmp.SetValue("kgPerMonth", 23.5m);

            factory.Save(tmp);

            Assert.IsNotNull(tmp.Id);

            var tmp2 = factory.GetByPath(profileId, "/home/waste", tmp.Id);
            Assert.IsNotNull(tmp2);

            factory.Delete(tmp.ProfileId, "/home/waste", tmp.Id);

            var tmp3 = factory.GetByPath(profileId, "/home/waste", tmp.Id);
            Assert.IsNull(tmp3);
        }
    }
}
